package com.c212.manager.controller;

import com.c212.manager.bean.UserInfoEntity;
import com.c212.manager.constants.Consts;
import com.c212.manager.repository.UserInfoRepository;
import com.c212.manager.utils.annotation.NotLogin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserInfoRepository userInfoRepository;


    @NotLogin
    @GetMapping("/login")
    public String login() {

        List<UserInfoEntity> lst = userInfoRepository.findAll();
        if (lst.size() <= 0) {
            UserInfoEntity userInfoEntity = new UserInfoEntity();
            userInfoEntity.setName("admin");
            userInfoEntity.setLoginId("admin");
            userInfoEntity.setLoginPwd("admin");
            userInfoEntity.setCreatedDate(LocalDateTime.now());

            userInfoRepository.save(userInfoEntity);

            // 权限自动添加



        }

        return "login";
    }

    @PostMapping("/shiro/login")
    public ModelAndView loginUser(String username,
                                  String password,
                                  HttpSession session,
                                  Map<String, Object> map) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            UserInfoEntity user = (UserInfoEntity) subject.getPrincipal();
            session.setAttribute("user", user);

            map.put("msg", "登录成功");
            map.put("url", "/article/list");
            return new ModelAndView("/common/success", map);
        } catch (Exception e) {
            map.put("msg", e.getMessage());
            map.put("url", "/user/login");
            return new ModelAndView("/common/error", map);
        }
    }

    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return "/user/login";
    }

    @NotLogin
    @PostMapping("/login")
    public ModelAndView loginPost(String username,
                                  String password,
                                  HttpServletRequest request,
                                  Map<String, Object> map) {

        if (username == null || password == null || username.length() <= 0 || password.length() <= 0) {
            map.put("msg", "请键入账号及密码信息");
            map.put("url", "/user/login");
            return new ModelAndView("/common/error", map);
        }

        UserInfoEntity userInfoEntity = userInfoRepository.findByLoginIdAndLoginPwd(username, password);
        if (userInfoEntity == null) {
            map.put("msg", "账号或密码错误");
            map.put("url", "/user/login");
            return new ModelAndView("/common/error", map);
        }

        //设置session信息
        request.getSession().setAttribute(Consts.USER_SESSION, userInfoEntity);

        map.put("msg", "登录成功");
        map.put("url", "/article/list");
        return new ModelAndView("/common/success", map);
    }

    @NotLogin
    @GetMapping("/exit")
    public String exit(HttpServletRequest request) {
        request.getSession().removeAttribute(Consts.USER_SESSION);
        return "login";
    }
}
